package com.shanghaionstar.user.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.UploadImgRes;
import com.shanghaionstar.user.iview.IMoreUpLoadImg;
import com.shanghaionstar.user.service.MoreUpLoadImgService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/22.
 */
public class MoreUpLoadImgPresenter extends BasePresenter<UploadImgRes> {
    private IMoreUpLoadImg       moreUpLoadImg;
    private MoreUpLoadImgService service;

    public MoreUpLoadImgPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<UploadImgRes> view) {
        super.setView(view);
        this.moreUpLoadImg = (IMoreUpLoadImg) view;
        service = new MoreUpLoadImgService(moreUpLoadImg.onActivity());
        setServiceProvider(service);
    }

    public void getData(String bitmapBytes, String extend) {
        moreUpLoadImg.setLoading(true);
        subscription=service.getObserval(bitmapBytes, extend).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<UploadImgRes>() {
                    @Override
                    public void call(UploadImgRes uploadImgRes) {
                        if (!CommonUtil.isNull(uploadImgRes)) {
                            moreUpLoadImg.setLoading(false);
                            moreUpLoadImg.onReponseSuccess(uploadImgRes);

                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        moreUpLoadImg.setLoading(false);
                        moreUpLoadImg.onResponseError((OnStarException) throwable);

                    }
                });
    }
}
